package janino.excessoes;

/**
 * For compatibility with pre-1.4 JDKs, this class mimics
 */
public class CausedException extends Exception {

    private Throwable optionalCause = null;

    public CausedException() {
    }

    public CausedException(String message) {
        super(message);
    }

    public CausedException(String message, Throwable optionalCause) {
        super(message);
        this.optionalCause = optionalCause;
    }

    public CausedException(Throwable optionalCause) {
        super(optionalCause == null ? null : optionalCause.getMessage());
        this.optionalCause = optionalCause;
    }

    public Throwable getCause() {
        return this.optionalCause;
    }

    public void printStackTrace() {
        super.printStackTrace();
        if (this.optionalCause == null) {
            return;
        }

        System.err.print("Caused by: ");
        this.optionalCause.printStackTrace();
    }
}
